package com.example.myapplication.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.Activity.DetailActivity;
import com.example.myapplication.Adapter.GuangGaoAdapter;
import com.example.myapplication.Adapter.HomeAdapter;
import com.example.myapplication.Bean.HomeBean;
import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
    ViewPager vpGuangGao;
    List<View> list = new ArrayList<>();
    List<HomeBean> mokuai_list=new ArrayList<>();

    View view1,view2,view3;
    GridView gvMoKuai;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what==10){
                //设置下一张广告页
                vpGuangGao.setCurrentItem((vpGuangGao.getCurrentItem()+1)%3);
                //重置小圆点选中状态
                yuanDianXuanZe();
                //设置选中圆点状态
                if (vpGuangGao.getCurrentItem()==0){
                    view1.setSelected(true);
                }else if(vpGuangGao.getCurrentItem()==1){
                    view2.setSelected(true);
                }else{
                    view3.setSelected(true);
                }
            }
        }
    };
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_home_fragment,null);

        initView(view);


        //实现轮播
        initLunBo();
        return view;
    }
    //实现轮播
    private void initLunBo() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {


                    while (!Thread.interrupted()){

                        Thread.sleep(2000);

                        handler.sendEmptyMessage(10);
                    }

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        thread.start();
    }


    private void initView(View view) {
        //初始化数据
        view1 = view.findViewById(R.id.view_dian1);
        view2 = view.findViewById(R.id.view_dian2);
        view3 = view.findViewById(R.id.view_dian3);
        gvMoKuai=view.findViewById(R.id.gv_home_mokuai);
        //默认所有小圆点都是灰色未选中
        yuanDianXuanZe();

        view1.setSelected(true);

        vpGuangGao = view.findViewById(R.id.vp_home_guanggao);


        View guanggao1 = LayoutInflater.from(getActivity()).inflate(R.layout.home_guangao1,null);
        View guanggao2 = LayoutInflater.from(getActivity()).inflate(R.layout.home_guangao2,null);
        View guanggao3 = LayoutInflater.from(getActivity()).inflate(R.layout.home_guangao3,null);
        //添加数据
        list.add(guanggao1);
        list.add(guanggao2);
        list.add(guanggao3);
        mokuai_list.add(new HomeBean(R.drawable.no1,"新品驾到"));
        mokuai_list.add(new HomeBean(R.drawable.no2,"食趣"));
        mokuai_list.add(new HomeBean(R.drawable.no3,"食品安全"));
        mokuai_list.add(new HomeBean(R.drawable.no4,"食品溯源"));
        mokuai_list.add(new HomeBean(R.drawable.no5,"健康养生"));
        mokuai_list.add(new HomeBean(R.drawable.no6,"产品展示"));
        //创建适配器
        GuangGaoAdapter adapter = new GuangGaoAdapter(list);
        HomeAdapter moKuaiAdapter=new HomeAdapter(mokuai_list,getActivity());
        //绑定适配器
        vpGuangGao.setAdapter(adapter);
        vpGuangGao.setCurrentItem(0);
        gvMoKuai.setAdapter(moKuaiAdapter);
        //点击事件
        guanggao1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), DetailActivity.class);
                intent.putExtra("imageIndex", 0); // 传递图片索引 0
                startActivity(intent);
            }
        });

        guanggao2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), DetailActivity.class);
                intent.putExtra("imageIndex", 1); // 传递图片索引 1
                startActivity(intent);
            }
        });

        guanggao3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), DetailActivity.class);
                intent.putExtra("imageIndex", 2); // 传递图片索引 2
                startActivity(intent);
            }
        });

    }

    private void yuanDianXuanZe() {
        view1.setSelected(false);
        view2.setSelected(false);
        view3.setSelected(false);
    }
}